import { createRouter, createWebHistory } from 'vue-router'
import icon1 from '../img/首页 2.png'
import icon2 from '../img/车次管理 2.png'
import icon3 from '../img/车站管理 2.png'
import icon4 from '../img/线路管理 2.png'
import icon5 from '../img/车票管理 2.png'
import icon6 from '../img/订单管理 2.png'
import icon7 from '../img/轨迹管理 2.png'


export const muta = [
  {
    path:'/home/homes',
    component:()=>import('../views/home/Homes.vue'),
    icon:icon1
  },
  {
    path:'/home/train',
    component:()=>import('../views/home/Train.vue'),
    icon:icon2
  },
  {
    path:'/home/station',
    component:()=>import('../views/home/Station.vue'),
    icon:icon3
  },
  {
    path:'/home/line',
    component:()=>import('../views/home/Line.vue'),
    icon:icon4
  },
  {
    path:'/home/tick',
    component:()=>import('../views/home/Tick.vue'),
    icon:icon5
  },
  {
    path:'/home/order',
    component:()=>import('../views/home/Order.vue'),
    icon:icon6
  },
  {
    path:'/home/personage',
    component:()=>import('../views/home/Personage.vue'),
    icon:icon7
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login', // 重定向:重新指向其它path,会改变网址
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/Login.vue')
    },
    {
      path: '/lead',
      name: 'lead',
      component: () => import('../views/lead.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/Home.vue'),
      children:muta
    },
  ]
})

//路由守卫
function getToken(){
  return window.localStorage.getItem("token");
}
  // 访问的白名单, 可以直接通行
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  // 访问的路径在白名单
  if (whiteList.includes(to.path.toLowerCase())) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      next('/login')
    }
  }
})


export default router
